import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STColumnBadge, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFSchemaEnum, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { ModalHelper } from '@delon/theme';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { of, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { BasicTableComponent } from 'src/app/routes/commom';
import { VehicleService } from '../../../vehicle/services/vehicle.service';
import { CarSettleCarauthComponent } from '../list/carauth/carauth.component';
@Component({
  selector: 'app-Vehicle-components-audit',
  templateUrl: './audit.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class VehicleComponentsAuditComponent extends BasicTableComponent implements OnInit {
  resourceStatus: any = 1;
  defaultTabs = 1;
  columns!: STColumn[];

  constructor(
    public service: VehicleService,
    private router: Router,
    private ar: ActivatedRoute,
    private modalHelper: ModalHelper,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const a: any = {};
    if (this.resourceStatus === 1) {
      a.approvalStatus = 10;
    } else if (this.resourceStatus === 2) {
      a.approvalStatus = 20;
    } else if (this.resourceStatus === 3) {
      a.approvalStatus = 30;
    }
    return {
      ...a,
      ...this.sf?.value
    };
  }

  get selectedRows() {
    return this.st?.list.filter(item => item.checked) || [];
  }

  ngOnInit() {
    this.initSF();
    this.initST();
  }

  search() {
    this.st?.load(1);
  }

  dataProcess(data: STData[]): STData[] {
    return data.map((i, index) => {
      i.showSortFlag = false;
      return i;
    });
  }
  initSF() {
    this.schema = {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        carNo: { title: '车牌号', maxLength: 9, type: 'string', ui: { showRequired: false, placeholder: '请输入车牌号' } },
        // carNo: {
        //   title: '车牌号',
        //   type: 'string',
        //   ui: {
        //     widget: 'select',
        //     serverSearch: true,
        //     searchDebounceTime: 300,
        //     searchLoadingText: '搜索中...',
        //     allowClear: true,
        //     onSearch: (q: any) => {
        //       if (!!q) {
        //         return this.service
        //           .request(this.service.$api_get_getCarLicenseListByCarNo_audit, {
        //             carNo: q
        //           })
        //           .pipe(map((res: any) => (res?.records as any[]).map(i => ({ label: i.carNo, value: i.carNo } as SFSchemaEnum))))
        //           .toPromise();
        //       } else {
        //         return of([]);
        //       }
        //     },
        //   } as SFSelectWidgetSchema
        // },
        carNoColor: {
          type: 'string',
          title: '车牌颜色',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'car:color' },
            containsAllLabel: true
          }
        },
        isSelf: {
          type: 'string',
          title: '是否挂靠',
          enum: [
            { label: '是', value: 1 },
            { label: '否', value: 0 }
          ],
          ui: {
            widget: 'select',
            allowClear: true
          }
        },
        saveUser: {
          type: 'string',
          title: '录入人员',
        }
        // approvalStatus: {
        //   type: 'string',
        //   title: '审核状态',
        //   enum: [
        //     { label: '未提交', value: '-1' },
        //     { label: '草稿', value: 0},
        //     { label: '待审核', value: 10 },
        //     { label: '审核通过', value: 20 },
        //     { label: '驳回', value: 30 },
        //     { label: '证件过期', value: 40 },
        //   ],
        //   default: '',
        //   ui: {
        //     widget: 'select',
        //     visibleIf: {
        //       expand: (value: boolean) => value,
        //     },
        //   },
        // },
        // approvalPassTime: {
        //   type: 'string',
        //   title: '审核通过时间',
        //   ui: {
        //     widget: 'sl-from-to-search',
        //     format: 'yyyy-MM-dd',
        //     placeholder: '请选择',
        //     nzShowTime: true,
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   } as SFDateWidgetSchema
        // }
      }
    };
  }

  initST() {
    this.columns = [
      // { title: '', type: 'checkbox', className: 'text-center' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '车牌号', width: '180px', className: 'text-center', index: 'carNo' },
      { title: '车辆品牌', width: '140px', className: 'text-center', index: 'carBrand' },
      { title: '车牌颜色', width: '140px', className: 'text-center', index: 'carNoColorLabel' },
      { title: '车型-车长-载重', width: '180px', className: 'text-center', render: 'carLength' },
      { title: '是否挂靠', width: '180px', className: 'text-center', render: 'isSelf' },
      { title: '所有人', width: '180px', className: 'text-center', index: 'carOwner' },
      { title: '录入人员', width: '180px', className: 'text-center', index: 'saveUser' },
      {
        title: '审核状态',
        className: 'text-center',
        index: 'approvalStatus',
        width: '180px',
        type: 'badge',
        badge: {
          '-1': { text: '未上传', color: 'default' },
          0: { text: '草稿', color: 'warning' },
          15: { text: '已撤销', color: 'warning' },
          10: { text: '待审核', color: 'warning' },
          20: { text: '已审核', color: 'success' },
          30: { text: '已驳回', color: 'error' },
          40: { text: '证件过期', color: 'error' }
        }
      },
      { title: '申请时间', width: '180px', className: 'text-center', index: 'createTime' },
      { title: '审核通过时间', width: '180px', className: 'text-center', index: 'approvalPassTime' },
      {
        title: '操作',
        fixed: 'right',
        width: '100px',
        className: 'text-center',
        buttons: [
          {
            text: '查看',
            acl: { ability: ['VEHICLE-AUDIT-view'] },
            click: item => {
              this.router.navigate(['./detail', item.id], { relativeTo: this.ar, queryParams: { carId: item.carId } });
              // this.router.navigate(['./view', item.id], { relativeTo: this.ar, queryParams: { tenantId: item.tenantId } });
            }
          }
        ]
      }
    ];
  }
  selectChange(e: number) {
    this.resourceStatus = e;
    this.initST();
    setTimeout(() => {
      this.st.load();
    }, 500);
  }
  daoyun(item: any) {
    this.router.navigate(['./view', item], { relativeTo: this.ar });
  }
  creat() {
    this.router.navigate(['./new'], { relativeTo: this.ar });
  }
  // 导出
  exportFire() {
    let params = Object.assign({}, this.reqParams || {});
    params.pageSize = -1;
    this.service.exportStart(params, this.service.$api_carLicenseAudit_export);
  }
  addModal() {
    const i = {
      appUserId: ''
    };
    this.modalHelper.create(CarSettleCarauthComponent, { i }, { size: 900 }).subscribe(res => {
      this.st.load();
    });
  }
}
